/*********************************************************************************
 *      Copyright:  (C) 2021 Simply myself<1239726156@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  my_cjson_parse.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2021年08月08日)
 *         Author:  Simply myself <1239726156@qq.com>
 *      ChangeLog:  1, Release initial version on "2021年08月08日 11时37分20秒"
 *                 
 ********************************************************************************/
 
#include "my_cjson_parse.h"

/* 打包json格式数据，使用到配置文件中的参数，标识符，以及具体数值 */
char *pack_cjon_float(struct payload_s *payload, char *identifier, float *value)
{
	int			 rv = 0;
	char		*string;
	cJSON		*cj_root	= NULL;
    cJSON		*cj_params	= NULL;
    cJSON		*cj_method	= NULL;
    cJSON		*cj_id		= NULL;
    cJSON		*cj_content	= NULL;
    cJSON		*cj_version	= NULL;
	
	if ( !payload || !identifier || !value )
	{
		printf("[ERROR]: Invalid parameter in %s\n\n", __FUNCTION__);
		return NULL;
	}
	
	cj_root = cJSON_CreateObject();
    cj_params = cJSON_CreateObject();
	
	if (!cj_root || !cj_params)
    {
		rv = -1;	goto cleanup;
    }
	
	cj_method = cJSON_CreateString(payload->method);
	if ( cj_method == NULL )
	{
		rv = -2;	goto cleanup;
	}
	
	cj_id = cJSON_CreateString(payload->id);
	if ( cj_id == NULL )
	{
		rv = -3;	goto cleanup;
	}
	
    cj_content = cJSON_CreateNumber(*value);
	if ( cj_content == NULL )
	{
		rv = -4;	goto cleanup;
	}	
	
	cj_version = cJSON_CreateString(payload->version);
	if ( cj_version == NULL )
	{
		rv = -5;	goto cleanup;
	}
	
	cJSON_AddItemToObject(cj_root, "method", cj_method);
	cJSON_AddItemToObject(cj_root, "id", cj_id);
	cJSON_AddItemToObject(cj_root, "params", cj_params);
	cJSON_AddItemToObject(cj_params, identifier, cj_content);
	cJSON_AddItemToObject(cj_root, "version", cj_version);
	
	string = cJSON_Print(cj_root);
	if ( string == NULL )
	{
		printf ("[ERROR]: Print cJSON failure\n");
		rv = -6;	goto cleanup;
	}
	
cleanup:
	cJSON_Delete(cj_root);
	if ( rv < 0 )
	{
		printf("[ERROR]: cJSON Packing failure rv[%d]\n\n", rv);
	}
	
	return string;
}

/* 解析接收到的payload，看是不是针对标识符 identifier 的消息，并将值返回到vlaue中 */
int parse_payload(char *payload, char *identifier, int *value)
{
	int          rv = 0;
	cJSON       *recv_payload = NULL;
	const char  *error_ptr;
	cJSON       *params = NULL;
	cJSON       *identity = NULL;
	char        *str;
	
	if ( !payload || !identifier || !value )
	{
		printf("[ERROR]: Invalid parameter in %s\n\n", __FUNCTION__);
		return -1;
	}
	
	/* 解析payload */
	recv_payload = cJSON_Parse(payload);
	if ( recv_payload == NULL )
	{
		printf ("[Error]: payload is NULL\n");
        rv = -2;	goto cleanup;
	}
	
	//printf ("payload: %s\n", payload);
	/* 提取 recv_payload JSON消息的"params"对象 */
	params = cJSON_GetObjectItemCaseSensitive(recv_payload, "params");
	if ( params == NULL )
	{
		printf ("[Error]: GetObject params failure\n");
		rv = -3;	goto cleanup;
	}
#if DEBUG
	str = cJSON_Print(params);
	printf ("params:%s\n\n", str);
#endif
	
	/* 从"params"对象中提取 identifier 的值 */
	identity = cJSON_GetObjectItemCaseSensitive(params, identifier);
	if ( !identity || !cJSON_IsNumber(identity) )
	{
		printf ("[Error]: identity is NULL or not a number\n");
		rv = -4;	goto cleanup;
	}
	*value = identity->valuedouble;
#if DEBUG
	str = cJSON_Print(identity);
	printf ("\"%s\": %s\n\n", identifier, str);
	printf ("value=%d\n\n", *value);
#endif
	
cleanup:
	cJSON_Delete(recv_payload);
	
	return rv;
}